Since you are using Fuse.js, you can take advantage of Extended Searching. This allows you to perform exact matches on specific fields, which is perfect for searching specific tag paths like Level1/Level2/Level3.
To make this work seamlessly, we need to ensure Fuse is configured to handle "Extended Search" and that your search index includes the full tag strings.

1. Update your Search Script

In your layout.njk, you need to enable the useExtendedSearch option in your Fuse initialization. This allows you to use special symbols (like quotes) to find exact paths.

JavaScript

// Inside your layout.njk search script
fetch('/search.json')
.then(response => response.json())
.then(data => {
fuse = new Fuse(data, {
keys: ['title', 'tags', 'date'],
threshold: 0.3,
useExtendedSearch: true, // This is the key for specific path searching
distance: 100
});
});

---

2. How to Search Specific Levels

Now that Extended Search is on, you can use the following "Power User" syntax in your search bar:

  • Exact Path Match: Wrap your path in double quotes.
    • Query: "Coding/Frontend/JavaScript"
    • Result: Only shows posts with that exact Level 3 tag.
  • Level Prefix Match: Use the ^ symbol to find tags that start with a specific level.
    • Query: ^Coding/
    • Result: Shows everything under the "Coding" Level 1.
  • Specific Sub-level: * Query: Frontend/JavaScript (without quotes)
    • Result: This uses your fuzzy logic to find that specific branch anywhere in the tag tree.

---

3. Improving the Search Index for Paths

To make path searching even more reliable, ensure your search-index.json.njk is outputting the tags as a clean array of strings.
File: search-index.json.njk

Code snippet


permalink: /search.json

[
{% for post in collections.post %}
{
"title": {{ post.data.title | dump | safe }},
"url": {{ post.url | dump | safe }},
"tags": {{ post.data.tags | dump | safe }},
"date": "{{ post.date | postDate }}"
}{% if not loop.last %},{% endif %}
{% endfor %}
]

4. Advanced: Auto-Complete Paths

If you find yourself searching for paths often, you can update your JavaScript to "suggest" paths when you type a forward slash /.

  1. When the user types /, the script looks at all available tags.
  2. It filters the tags to show only those containing that path.
  3. The user can click one to instantly fill the search bar.

---

Summary of Search Syntax

To find... Type this into Search Logic
Anything related JavaScript Fuzzy match (includes typos)
Exact Level Path "Coding/JS" Exact match (includes quotes)
Starting Level ^Coding Starts with "Coding"
Ending Level JS$ Ends with "JS"

Would you like me to update your JavaScript to automatically wrap searches in quotes if it detects a forward slash, making path searching easier for your readers?